package com.yxhl.zoume.core.car.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.afollestad.materialdialogs.MaterialDialog;
import com.yxhl.protobuf.MpvValuationRules;
import com.yxhl.protobuf.OrderBizType;
import com.yxhl.zoume.R;
import com.yxhl.zoume.common.ui.fragment.BaseFragment;
import com.yxhl.zoume.core.car.activity.CarPricingRulesActivity;
import com.yxhl.zoume.core.car.presenter.QueryPricingRulesPresenter;
import com.yxhl.zoume.core.car.view.PricingRulesView;
import com.yxhl.zoume.core.func.webpage.info.WebPageEntrance;
import com.yxhl.zoume.di.component.car.ExpressCarComponent;
import com.yxhl.zoume.utils.PromptUtils;
import com.yxhl.zoume.utils.ZMDialog;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import me.relex.circleindicator.CircleIndicator;

/**
 * Author：zhongshan   
 * Date：2016/12/15 17:12      
 * Description：
 */
public class CarPricingRulesFragment extends BaseFragment implements PricingRulesView {

    @BindView(R.id.viewpager)
    ViewPager viewpager;
    @BindView(R.id.indicatorView)
    CircleIndicator indicatorView;

    @Inject
    QueryPricingRulesPresenter queryPricingRulesPresenter;

    private static final String ARGUMENT_KEY_BIZ_TYPE = "argument_key_biz_type";
    private static final String ARGUMENT_KEY_VEHICLE_TYPE = "argument_key_vehicle_type";
    private static final String ARGUMENT_KEY_PART_TIME = "argument_key_part_time";

    private CarPricingRulesActivity mActivity;
    private OrderBizType bizType;
    private String vehicleType;
    private int partTime;
    private MaterialDialog loadingDialog;
    private List<MpvValuationRules> rulesList = new ArrayList<>();
    private PricingRulesFragmentPagerAdapter adapter;

    public static CarPricingRulesFragment newInstance(OrderBizType bizType, String vehicleType, int partTime) {
        CarPricingRulesFragment fragment = new CarPricingRulesFragment();
        Bundle bundle = new Bundle();
        bundle.putString(ARGUMENT_KEY_VEHICLE_TYPE, vehicleType);
        bundle.putSerializable(ARGUMENT_KEY_BIZ_TYPE, bizType);
        bundle.putInt(ARGUMENT_KEY_PART_TIME, partTime);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected void initializeInjector() {
        ExpressCarComponent component = getComponent(ExpressCarComponent.class);
        component.inject(this);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_car_pricing_rules;
    }

    @Override
    protected void setupToolBar() {
        mActivity.setToolbarTitle("计价规则");
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity = (CarPricingRulesActivity) getActivity();
        queryPricingRulesPresenter.attachView(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        parseParams();
        initViewControl();
    }

    private void parseParams() {
        Bundle arguments = getArguments();
        if (arguments != null) {
            bizType = (OrderBizType) arguments.getSerializable(ARGUMENT_KEY_BIZ_TYPE);
            vehicleType = arguments.getString(ARGUMENT_KEY_VEHICLE_TYPE);
            partTime = arguments.getInt(ARGUMENT_KEY_PART_TIME, 0);
            queryPricingRules();
        }
    }

    @OnClick(R.id.tv_detailed_desc)
    void onDetailClick() {
        getAppComponent().navigator().navigateToWebPageActivity(mActivity, WebPageEntrance.PRICE_RULES_DETAIL);
    }

    private void queryPricingRules() {
        queryPricingRulesPresenter.queryPricingRules(bizType, partTime);
    }

    private void initViewControl() {
        adapter = new PricingRulesFragmentPagerAdapter();
        viewpager.setAdapter(adapter);
        indicatorView.setViewPager(viewpager);
        adapter.registerDataSetObserver(indicatorView.getDataSetObserver());
    }

    @Override
    public void showQueryLoading() {
        loadingDialog = ZMDialog.getLoadingDialog(mActivity);
        loadingDialog.show();
    }

    @Override
    public void queryPricingRulesSuccess(List<MpvValuationRules> lists) {
        rulesList = lists;
        adapter.notifyDataSetChanged();
        for (int i = 0; i < lists.size(); i++) {
            if (lists.get(i).getModelType().equals(vehicleType)) {
                viewpager.setCurrentItem(i);
            }
        }
    }

    @Override
    public void queryPricingRulesFailure(String message) {
        PromptUtils.show(mActivity, message, Toast.LENGTH_SHORT);
    }

    @Override
    public void queryError() {
        PromptUtils.showShort(mActivity, R.string.net_error);
    }

    @Override
    public void disMissQueryLoading() {
        if (loadingDialog != null)
            loadingDialog.dismiss();
    }

    private class PricingRulesFragmentPagerAdapter extends FragmentStatePagerAdapter {

        public PricingRulesFragmentPagerAdapter() {
            super(mActivity.getSupportFragmentManager());
        }

        @Override
        public Fragment getItem(int position) {
            if (rulesList != null && rulesList.size() > 0) {
                return CarPricingRulesFillFragment.newInstance(rulesList.get(position));
            } else
                return null;
        }

        @Override
        public int getCount() {
            return rulesList.size();
        }
    }
}
